<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
		<meta name="format-detection" content="telephone=no,email=no,date=no,address=no">
		<title>系统设置</title>
		<link rel="stylesheet" type="text/css" href="../css/aui.2.0.css" />
		<style>
			body {
			}
			p {
				margin-bottom: 15px;
			}

		</style>
	</head>
	<body>
		<header class="aui-bar aui-bar-nav" style="background-color: #03a9f4">
		    <div class="aui-title" style="padding-top: 10px;">
		    	<!-- <img src='../image/logo-smaller.png'/> -->
		    </div>
		</header>

		<div class="aui-content aui-margin-b-15">
			<ul class="aui-list aui-form-list">
				<li class="aui-list-item">
	                <div class="aui-list-item-inner">
	                    <div class="aui-list-item-label">内网IP</div>
	                    <div class="aui-list-item-input">
	                        <input type="text" placeholder="IP地址" id="serverIp" value="192.168.1.108"/>
	                    </div>
	                </div>
	            </li>
	            <li class="aui-list-item">
	                <div class="aui-list-item-inner">
	                    <div class="aui-list-item-label">内网端口号</div>
	                    <div class="aui-list-item-input">
	                        <input type="text" placeholder="如8080" id="serverPort" value="8050"/>
	                    </div>
	                </div>
	            </li>
	            <li class="aui-list-item">
	                <div class="aui-list-item-inner">
	                    <div class="aui-list-item-label">内网服务名</div>
	                    <div class="aui-list-item-input">
	                        <input type="text" placeholder="服务器访问名称" id="serverProject" value="htsys"/>
	                    </div>
	                </div>
	            </li>
            </ul>
		</div>

		<div class="aui-content aui-margin-b-15">
			<ul class="aui-list aui-form-list">
				<li class="aui-list-item">
	                <div class="aui-list-item-inner">
	                    <div class="aui-list-item-label">外网IP</div>
	                    <div class="aui-list-item-input">
	                        <input type="text" placeholder="IP地址" id="serverIpW" value="192.168.1.108"/>
	                    </div>
	                </div>
	            </li>
	            <li class="aui-list-item">
	                <div class="aui-list-item-inner">
	                    <div class="aui-list-item-label">外网端口号</div>
	                    <div class="aui-list-item-input">
	                        <input type="text" placeholder="如8080" id="serverPortW" value="8050"/>
	                    </div>
	                </div>
	            </li>
	            <li class="aui-list-item">
	                <div class="aui-list-item-inner">
	                    <div class="aui-list-item-label">外网服务名</div>
	                    <div class="aui-list-item-input">
	                        <input type="text" placeholder="服务器访问名称" id="serverProjectW" value="htsys"/>
	                    </div>
	                </div>
	            </li>
            </ul>
		</div>

		<div class="aui-content aui-margin-b-15">
			<ul class="aui-list aui-form-list">
				<li class="aui-list-item">
	                <div class="aui-list-item-inner">
	                    <div class="aui-list-item-label">网络选择</div>
	                    <div class="aui-list-item-input">
			                <label><input class="aui-radio" type="radio" id="inRadio" name="netRadio" checked value="in" onclick="doSelectNet()">内网</label>
			                <label><input class="aui-radio" type="radio" id="outRadio" name="netRadio" value="out" onclick="doSelectNet()">外网</label>
			            </div>
	                </div>
	            </li>
            </ul>
		</div>

		<div class="aui-content-padded">
			<p><div class="aui-btn aui-btn-info aui-btn-block" onclick='pingTest()'>网络检测</div></p>
		</div>

		<div class="aui-content-padded">
			<p><div class="aui-btn aui-btn-success aui-btn-block" onclick='doSave()'>保存</div></p>
		</div>

	</body>
	<script type="text/javascript" src="../script/api.js"></script>
    <script type="text/javascript" src="../script/jquery-1.8.2.min.js"></script>
	<script type="text/javascript">
		apiready = function() {
			var serverIpN = localStorage.getItem('serverIpN');
			var serverPortN = localStorage.getItem('serverPortN');
			var serverProjectN = localStorage.getItem('serverProjectN');
			var serverIpW = localStorage.getItem('serverIpW');
			var serverPortW = localStorage.getItem('serverPortW');
			var serverProjectW = localStorage.getItem('serverProjectW');

			var netChoice = localStorage.getItem('netChoice');

			if(serverIpN!=null || serverPortN!=null || serverProjectN!=null)
			{
				$("#serverIp").val(serverIpN);
				$("#serverPort").val(serverPortN);
				$("#serverProject").val(serverProjectN);
			}

			if(serverIpW!=null || serverPortW!=null || serverProjectW!=null)
			{
				$("#serverIpW").val(serverIpW);
				$("#serverPortW").val(serverPortW);
				$("#serverProjectW").val(serverProjectW);
			}

			if(netChoice != null && netChoice == "in")
			{
//				//内网
//				if(serverIp!=null || serverPort!=null || serverProject!=null)
//				{
//					$("#serverIp").val(localStorage.getItem('serverIp'));
//					$("#serverPort").val(localStorage.getItem('serverPort'));
//					$("#serverProject").val(localStorage.getItem('serverProject'));
					document.getElementById('inRadio').checked=true;
					selectInNet();
//				}
			}else if(netChoice != null && netChoice == "out"){
//				//外网
//				if(serverIp!=null || serverPort!=null || serverProject!=null)
//				{
//					$("#serverIpW").val(localStorage.getItem('serverIp'));
//					$("#serverPortW").val(localStorage.getItem('serverPort'));
//					$("#serverProjectW").val(localStorage.getItem('serverProject'));
					document.getElementById('outRadio').checked=true;
					selectOutNet();
//				}
			}
		};
		function closeWin() {
			api.closeWin();
		}

		function doSelectNet()
		{
			var obj  = document.getElementsByName('netRadio');
	        for(var i=0;i<obj.length;i++)
	        {
	            if(obj[i].checked==true)
	            {
	                if(obj[i].value=='in')
	                {
	                	//选择内网
	                	selectInNet();
	                }else if(obj[i].value=='out')
	                {
	                	//选择外网
	                    selectOutNet();
	                }
	            }
	        }
		}

		function selectInNet()
		{
			document.getElementById("serverIp").disabled=false;
        	document.getElementById("serverPort").disabled=false;
        	document.getElementById("serverProject").disabled=false;

        	document.getElementById("serverIpW").disabled=true;
        	document.getElementById("serverPortW").disabled=true;
        	document.getElementById("serverProjectW").disabled=true;
		}

		function selectOutNet()
		{
			document.getElementById("serverIpW").disabled=false;
        	document.getElementById("serverPortW").disabled=false;
        	document.getElementById("serverProjectW").disabled=false;

        	document.getElementById("serverIp").disabled=true;
        	document.getElementById("serverPort").disabled=true;
        	document.getElementById("serverProject").disabled=true;
		}

		function pingTest()
		{
			//获取当前选择网络，并检测网络连接是否成功
			var obj  = document.getElementsByName('netRadio');
			var serverIp = "";

			for(var i=0;i<obj.length;i++)
	        {
	            if(obj[i].checked==true)
	            {
	                if(obj[i].value=='in')
	                {
	                	serverIp = $("#serverIp").val();
	                }else if(obj[i].value=='out')
	                {
	                	serverIp = $("#serverIpW").val();
	                }
               	}
           	}
           	//alert(serverIp);
           	var modulePing = api.require('modulePing');
           	api.showProgress({
			    title: '网络检测中...'
			});
           	modulePing.pingtest({
			    ip: serverIp
			}, function(ret, err){
				console.log(JSON.stringify(ret));
				api.hideProgress();
			    if(ret){
			         if(ret.okNum > 0)
			         {
			         	api.toast({
	                         msg:'网络通畅'
                        });
			         }else{
			         	api.toast({
	                         msg:'网络不通'
                        });
			         }
			    }else{
			         alert(JSON.stringify(err));
			    }
			});

		}

		function doSave() {

			var serverIpNO = localStorage.getItem('serverIpN');
			var serverPortNO = localStorage.getItem('serverPortN');
			var serverProjectNO = localStorage.getItem('serverProjectN');
			var serverIpWO = localStorage.getItem('serverIpW');
			var serverPortWO = localStorage.getItem('serverPortW');
			var serverProjectWO = localStorage.getItem('serverProjectW');

			var netRadioObj = document.getElementsByName("netRadio");  //这个是以标签的name来取控件
			var netRadioStr = "";

			for(j=0; j<netRadioObj.length;j++)
			{
			 	if(netRadioObj[j].checked){
			 		netRadioStr = netRadioObj[j].value;
			   	}
			}

			//alert(netRadioStr);

			var serverIp = "";
			var serverPort = "";
			var serverProject = "";


			localStorage.setItem('netChoice', netRadioStr);
			//保存内网配置信息
			serverIp = $("#serverIp").val();
			serverPort = $("#serverPort").val();
			serverProject = $("#serverProject").val();

			localStorage.setItem('serverIpN', serverIp);
			localStorage.setItem('serverPortN', serverPort);
			localStorage.setItem('serverProjectN', serverProject);
			//保存外网配置信息
			serverIp = $("#serverIpW").val();
			serverPort = $("#serverPortW").val();
			serverProject = $("#serverProjectW").val();

			localStorage.setItem('serverIpW', serverIp);
			localStorage.setItem('serverPortW', serverPort);
			localStorage.setItem('serverProjectW', serverProject);
			var clearFlag = 'N';
			//判断网络设置是否修改有修改则清空工作中心缓存
			if($("#serverIp").val() != serverIpNO)
			{
				clearFlag = 'Y';
			}
			if($("#serverPort").val() != serverPortNO)
			{
				clearFlag = 'Y';
			}
			if($("#serverProject").val() != serverProjectNO)
			{
				clearFlag = 'Y';
			}
			if($("#serverIpW").val() != serverIpWO)
			{
				clearFlag = 'Y';
			}
			if($("#serverPortW").val() != serverPortWO)
			{
				clearFlag = 'Y';
			}
			if($("#serverProjectW").val() != serverProjectWO)
			{
				clearFlag = 'Y';
			}

			if( clearFlag == 'Y' )
			{
				localStorage.removeItem('workcenterGid');
				localStorage.removeItem('workcenterCode');
				localStorage.removeItem('workcenterName');
			}

			//保存到缓存中的内容，同步写到配置文件ipConfig中
			var ipData = "serverIpN="+$("#serverIp").val()
				+",serverPortN="+$("#serverPort").val()
				+",serverProjectN="+$("#serverProject").val()
				+",serverIpW="+$("#serverIpW").val()
				+",serverPortW="+$("#serverPortW").val()
				+",serverProjectW="+$("#serverProjectW").val();

			console.log("ipData###"+ipData);
			api.writeFile({
			    path: 'fs://ipConfig.txt',
			    data: ipData
			}, function(ret, err) {
			    if (ret.status) {
			        console.log("写入数据成功");
			        api.rebootApp();
			    } else {
			    	var errMsg = "写入数据失败：" +err.msg+","+err.code
					console.log(errMsg);
					api.toast({
	                    msg:errMsg
                    });
			    }
			});



		}
	</script>
</html>
